package com.dzf.controller;

import com.dzf.entity.*;
import com.dzf.service.OrdService;
import com.dzf.util.SessionUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author DZF
 * 订单的前台接口
 */
@RequestMapping("/api/fore")
@RestController
public class ForeController {
    @Autowired
    SessionUtil sessionUtil;

    @Autowired
    OrdService ordService;

    //前端接口

    /**
     * 获取当前用户的所有订单
     * @param map 条件集合
     * @return 查询的结果 使用OrdLines组合类
     */
    @RequestMapping("/ord/findOrdLines")
    public RespJson findOrdLines(@RequestBody Map<String,Object> map)
    {
        map.put("uid",sessionUtil.getUser().getId());

        List<OrderLine> lines = ordService.findOrdLines(map);
        return RespJson.pack(200,"ok",lines);
    }

    /**
     * 查询某个用户的订单头
     * @param map   条件
     * @param start 起始页
     * @param num   每页的数据
     * @return      查询的结果
     */
    @RequestMapping("/ord/findOrdLineByPage/{start}/{num}")
    public RespJson findOrdLineByPage(@RequestBody Map<String,Object> map,@PathVariable("start") int start,@PathVariable("num")int num)
    {

        map.put("uid",sessionUtil.getUser().getId());
        map.put("start",(start*num)-num);
        map.put("num",num);

        return RespJson.pack(200,"ok",ordService.findOrdInfo(map));
    }

    /**
     * 购物车中的数据转订单
     * @param carts 当前用户选中的购物车数据
     * @return 转化后的订单详细
     */
    @RequestMapping("/ord/addCart2Order")
    public RespJson addOrdItem(@RequestBody List<Cart> carts){
        int sum = 0;

        for (Cart cart:carts) {
            sum += (cart.getNum()*cart.getBook().getPrice());
        }//把总价算出。amount
        MyUser user = (MyUser) sessionUtil.getUser();
        System.out.println(user);
        Order ord = new Order();
        ord.setUid(user.getId());
        ord.setIspay(0);
        ord.setPhone(user.getPhone());
        ord.setAmount(sum);
        ord.setTcreate(new Date());
        OrdInfo ordInfo = ordService.addOrdItem(user, ord, carts);
        return RespJson.pack(200,"OK",ordInfo);
    }

    /**
     * 查询某个订单的订单详细
     * @param ordid 订单ID
     * @return 查询的结果
     */
    @RequestMapping("/ord/findItemInfoByOrdid/{ordid}")
    public RespJson findItemInfoByOrdid(@PathVariable("ordid") int ordid){
        List<ItemBook> itemBooks = ordService.findItemInfoByOrdid(ordid);
        return RespJson.pack(200,"OK",itemBooks);
    }



    //前端后端

    /**
     * 获取某一个订单的全部商品详情 -》包含订单头信息
     * @param ordid 订单ID
     * @return  查询的结果
     */
    @RequestMapping("/ord/findInfoByOrdid/{ordid}")
    public RespJson findInfoByOrdid(@PathVariable("ordid")int ordid)
    {
        MyUser user = (MyUser) sessionUtil.getUser();
        OrdInfo info = ordService.findInfoByOrdid(user.getId(), ordid);
        System.err.println(info);
        return RespJson.pack(200,"ok",info);
    }

    /**
     *  添加发货地址
     * @param order 订单的实体类
     * @return
     */
    @RequestMapping("/ord/addAddr")
    public RespJson addAddr(@RequestBody Order order)
    {
        ordService.addAddr(order);
        return RespJson.pack(200,"OK","");
    }


    /**
     * 修改订单支付信息 --》是否支付
     * @param ord 订单实体类
     * @return
     */
    @RequestMapping("/ord/addPay")
    public RespJson addPay(Order ord){
        ordService.addPay(ord);
        return RespJson.pack(200,"OK",null);
    }


    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}
